// pages/shoop/shoop.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: true,
    //搜索内容
    searchVal: '',
    imgUrls: [
      'cloud://wx-shopping-zbs.7778-wx-shopping-zbs-1259289821/33.jpg',
      'cloud://wx-shopping-zbs.7778-wx-shopping-zbs-1259289821/11.jpg',
      'cloud://wx-shopping-zbs.7778-wx-shopping-zbs-1259289821/22.jpg',
    ],
    Goods:[],

    indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 1000,
    circular: true,
    currentTab: 0,

    windowHeight:0
  },


  clickTab: function(e) {
    console.log(e.target.dataset.current)
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  input(e) {
    this.setData({
      searchVal: e.detail.value
    })
    console.log(e.detail.value)
  },
  clear: function () {
    this.setData({
      searchVal: ""
    })
  },
  //模糊搜索
  search: function () {
    wx.navigateTo({
      url: "/pages/indexType/Type?search=搜索",
    })
  },
  type:function(e){
    console.log(e.currentTarget.dataset.id)
    if(e.currentTarget.dataset.id=="上装"){
      wx.navigateTo({
        url: "/pages/indexType/Type?type=男:衣服&type2=女:衣服&search=分类",
      })
    }
    if(e.currentTarget.dataset.id=="下装"){
      wx.navigateTo({
        url: "/pages/indexType/Type?type=男:裤子&type2=女:衣服&search=分类",
      })
    }
    if(e.currentTarget.dataset.id=="鞋子"){
      wx.navigateTo({
        url: "/pages/indexType/Type?type=男:鞋子&type2=女:鞋子&search=分类",
      })
    }
    if(e.currentTarget.dataset.id=="饰品"){
      wx.navigateTo({
        url: "/pages/indexType/Type?type=男:其他&type2=女:其他&search=分类",
      })
    }
    // this.GoodsType(e.target.dataset.id)
  },

  commodity:function(e){
    console.log(this.data.Goods[e.currentTarget.dataset.id].id)
    console.log(this.data.Goods[e.currentTarget.dataset.id].liulan)
    var that=this
    wx.cloud.callFunction({
      name: "Browse",
      data: {
        liulan: that.data.Goods[e.currentTarget.dataset.id].liulan,
        id:that.data.Goods[e.currentTarget.dataset.id].id,
      }
    }).then(res => {
      console.log(res.data);
    }).catch(err => {
      console.log(err);
    })

    wx.navigateTo({
      url: "/pages/commodity/commodity?commodity=" + that.data.Goods[e.currentTarget.dataset.id].id + "",
    })
  },
  // 查询商品
  GoodsType:function(shoop){
    var a=this.data.Goods.length
    db.collection("Goods").where({
      type:shoop
    }).get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        
        for (let i = a; i < res.data.length;i++){
          var id = "Goods[" + i + "].id"
          var title = "Goods[" + i + "].title"
          var fileid = "Goods[" + i + "].fileid"
          var rmb = "Goods[" + i + "].rmb"
          var sale = "Goods[" + i + "].sale"
          var liulan = "Goods[" + i + "].liulan"
          var sell = "Goods[" + i + "].sell"
          this.setData({
            [title]: res.data[i].title,
            [id]: res.data[i]._id,
            [fileid]: res.data[i].fileIDs[0],
            [rmb]: res.data[i].rmb,
            [sale]: res.data[i].sale,
            [liulan]: res.data[i].liulan,
            [sell]: res.data[i].sell,

          })
          console.log( this.data.Goods)
        }
       
        
      })
      .catch(err => {
        console.error(err)
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this 
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight)
          that.setData({
              windowHeight: res.windowHeight
          });
      }
    });
  },
  getGoods:function(){
    var that=this
    var a=that.data.Goods.length
    db.collection("Goods").get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        
        for (let i = a; i < res.data.length;i++){
          var id = "Goods[" + i + "].id"
          var title = "Goods[" + i + "].title"
          var fileid = "Goods[" + i + "].fileid"
          var rmb = "Goods[" + i + "].rmb"
          var rmb2 = "Goods[" + i + "].rmb2"
          var sale = "Goods[" + i + "].sale"
          var liulan = "Goods[" + i + "].liulan"
          var sell = "Goods[" + i + "].sell"
          that.setData({
            [title]: res.data[i].title,
            [id]: res.data[i]._id,
            [fileid]: res.data[i].fileIDs[0],
            [rmb]: res.data[i].rmb,
            [rmb2]: (res.data[i].rmb/10.0*res.data[i].sale).toFixed(2),
            [sale]: res.data[i].sale,
            [liulan]: res.data[i].liulan,
            [sell]: res.data[i].sell,
          })

        }
       console.log( that.data.Goods)
        
      })
      .catch(err => {
        console.error(err)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      Goods:[]
    })
    this.getGoods()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})